package ua.form;

import java.util.HashSet;
import ua.model.RoleBean;
import ua.model.UserBean;
import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class UserForm extends ActionForm 
{
    UserBean userBean=new UserBean();
	private String[] roleselected;
	
	public String getId(){
		return userBean.getId();
	} 
	
	public void setId(String roleid){
		userBean.setId(roleid);
	}
	
	public String[] getRoleselected()
	{
	  return roleselected;	
	}
	
	public void setRoleselected(String[] role)
	{
		roleselected=role;
	}
	                  
  	public UserBean getUser() {
		return userBean;
	}
  	
	public void setUser(UserBean user) {
		this.userBean = user;
		this.setId(user.getUsername());
		int item=0;
		roleselected=new String[user.getRoles().size()];
		for (RoleBean roleBean:user.getRoles())  
		roleselected[item++]=roleBean.getRolename();
	}
	
	@Override
	public boolean equals(Object arg0) {
		return userBean.equals(arg0);
	}
	
    public String getPassword() {
  		return userBean.getPassword();
  	}
    
    public void setPassword(String password) {
  		this.userBean.setPassword(password);
  	}
    
    public HashSet<RoleBean> getRoles() {
  	    return this.userBean.getRoles();
  	}
  	
    public void setRoles(HashSet<RoleBean> roles) {
  		this.userBean.setRoles(roles);
  	}
  	
	public void setRolesStr(String strRoles) {
	    this.userBean.setRolesStr(strRoles);
    }
 	
    public String getRolesStr() {
	    return userBean.getRolesStr();
    }
 	
  	public void setUsername(String name) {
		this.userBean.setUsername(name);
	}
  		
	public String getUsername() {
		return userBean.getUsername();
	}
	
	public String save()
	{	
		for (String element: roleselected)
		userBean.addRoleBean(new RoleBean(element));	  
	   return userBean.save();
	}

}
